/*
两头堵模型??
如果一个字符串是这样  "       abcdfg       ",求中间非空字符的个数
原理:从开始地址和结束地址开始加减,遇到非空之后将他们地址相减
strncpy(arr1,arr2,n),该函数会将从arr2为首地址字符串的前n位字符拷贝给arr1地址所在的空间
*/
# include<stdio.h>
# include<string.h>
// # include<ctype.h>
int main1 (void)
{
	char *p = "    abcdfg      ";
	int begin = 0;
	int len = 0;
	int end = strlen(p) -1 ;//数组是从0开始的
//	printf ("%d %d",begin,end);
	while (p[begin] == ' ' && p[begin] != 0)//请时刻記住= 和== 的区别 ok?
	{
		begin ++;
	}
	while (p[end] == ' ' && p[end] != 0 )
	{
		end --;
	}
	printf ("%d %d",begin,end);
	len = end - begin +1;
	printf ("%d\n",len);

	return 0;
}
int ret_num(char *p,char *q)
{
	if ( p == NULL)
		return -1;
	int begin = 0;
	int len = 0;
	int end = strlen(p) -1 ;
	while (p[begin] == ' ' && p[begin] != 0)
	{
		begin ++;
	}
	while (p[end] == ' ' && p[end] != 0 )
	{
		end --;
	}
	int n = end -begin +1;
	strncpy( q,p+begin,n);//代替了下面的语句
	//printf ("%d %d",begin,end);
/*	int a;
	for (int i = begin, a= 0 ;i <= end;i++,a++)//实现了将中间这些字母拷贝出来的功能
	{
		q[a] = p [i];
	}*/
	len = end - begin +1;
	return len;
}
int main (void)
{
	char *p = "    abcdefg        ";
	char arr[100] = {0};
	char *q = arr;
	int len = ret_num(p,q);
	if (len == -1)
		printf ("请输入一个有效字符串\n");
	else
		printf ("%d\n",len);
	printf ("%s\n",q);
	return 0;

}
